// packageB/pages/package/liangdeng/bind_positon/bind_positon.js
const mybehavior = require("../../../../../behaviors/requests")
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/order_system",
    company: "",
    manager: "",
    line: "",
    position: "",
    position_list: [],
    position_list1: [],
    label_mac: "",
    label_list: [],
    indicator_mac: "",
    indicator_list: [],
    options: [],
    options1: [],
    options11: [],
    search_value: "",
    options2: ['工艺部', '物流管理中心', '东区家用总装工厂', '西区家用总装工厂', '商用总装工厂',],
    options22: [],
    search_value2: "",
    options3: [],
    popup_show1: false,
    popup_show2: false,
    popup_show3: false,
    radio: "1",
    radio_show: true
  },
  // 选择方式
  onRadioChange(event) {
    if (event.detail == "1") {
      this.setData({
        radio_show: true,
        radio: event.detail,
      });
    } else {
      this.setData({
        radio_show: false,
        radio: event.detail,
      });
    }

  },
  onSearch1(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options11
      this.setData({
        options1: arr
      })
    } else {
      let list1 = this.data.options11
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options1: arr
      })
    }
  },
  onSearch2(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options22
      this.setData({
        options2: arr
      })
    } else {
      let list1 = this.data.options22
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value2) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options2: arr
      })
    }
  },
  // 点击--公司名称--选取输入框
  onClick1(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show1: true
    })
  },
  // 选择--公司名称--输入框关闭
  onClose1() {
    this.setData({ popup_show1: false });
  },
  // 确认--公司名称值--弹出框关闭
  onChange1(event) {
    const { picker, value, index } = event.detail;
    const arr = this.data.options[value]
    this.setData({
      company: value,
      options2: arr,
      options22: arr,
      popup_show1: false,
      manager: "",
      line: "",
    })
  },

  // 点击--单位--选取输入框
  onClick2(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show2: true
    })
  },
  // 选择--单位--输入框关闭
  onClose2() {
    this.setData({ popup_show2: false });
  },
  // 确认--单位--弹出框关闭
  onChange2(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      manager: value,
      line: "",
      popup_show2: false,
    })
    this.getLineDatas()
  },



  // 获取--公司名称--数据
  getCompanyDatas() {
    let url = this.data.baseUrl + "/get_u_permission"
    let data = {

    }
    this.post(url, data).then(res => {
      console.log("公司名称", res.data)
      let data_list = Object.keys(res.data)
      let company1 = wx.getStorageSync('company')
      this.setData({
        company: company1,

      })
      let arr = this.data.options2.concat(res.data[company1])
      const uniqueArray = Array.from(new Set(arr))
      if (res.code == 20000) {

        this.setData({
          options1: data_list,
          options11: data_list,
          options: res.data,
          options2: uniqueArray,
          options22: uniqueArray,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  // 点击--仓库选取--选取输入框
  onClick3(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show3: true
    })
  },
  // 选择--仓库选取--输入框关闭
  onClose3() {
    this.setData({ popup_show3: false });
  },
  // 确认--仓库选取--弹出框关闭
  onChange3(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      line: value,
      popup_show3: false,
    })
  },

  // 
  // 获取--仓库选取--数据
  getLineDatas() {
    let url = this.data.baseUrl + "/positions_lines"
    let data = {
      company: this.data.company,
      manager: this.data.manager,
    }
    this.post(url, data).then(res => {
      console.log("仓库选取", res.data)

      if (res.code == 20000) {

        this.setData({
          options3: res.data,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  // 灭灯操作
  offDeng() {
    wx.showModal({
      title: '一键灭灯',
      content: '灭灯会影响仓库拣选任务的亮灯指引，请再次确认！',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          let url = this.data.baseUrl + "/refresh_success_batch_c"
          let data = {
            company: this.data.company,
            manager: this.data.manager,
            line: this.data.line,
          }
          this.post(url, data).then(res => {
            console.log("灭灯操作", res)

            if (res.code == 20000) {
          
              Dialog.confirm({
                title: res.msg,
                confirmButtonText: '查看灭灯数据'
              })
                .then(() => {
           
                  wx.navigateTo({
                    url: '/packageB/pages/package/liangdeng/off_deng/off_deng_data/off_deng_data',
                    success(res) {
                      res.eventChannel.emit('acceptDataFromOpenerPage', data)
          
                    }
                  })
              
                })
                .catch(() => {
                  // on cancel
                });




            } else {
              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
          }).catch(err => {
            console.log(err);
            wx.showToast({
              title: err.msg,
              icon: 'error'
            })
          })
        }
      }
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCompanyDatas()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})